标题:一年中关于C的总结
只看楼主
madfrogme
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
结帖率:98.63%
 问题点数:0 回复次数:2 
一年中关于C的总结
#include <pthread.h>
#include <stdio.h>
/* Parameters to print_function.  */
struct char_print_parms
{
    /* The character to print.  */
    char character;
    /* The number of times to print it.  */
    int count;
};
    /* Prints a number of characters to stderr, as given by PARAMETERS,
    which is a pointer to a struct char_print_parms.  */
void* char_print (void* parameters)
{
    /* Cast the cookie pointer to the right type.  */
    struct char_print_parms* p = (struct char_print_parms*) parameters;
    int i;
    for (i = 0; i < p->count; ++i)
    fputc (p->character, stderr);
    return NULL;
}
    /* The main program.  */
int main ()
{
    pthread_t thread1_id;
    pthread_t thread2_id;
    struct char_print_parms thread1_args;
    struct char_print_parms thread2_args;
    /* Create a new thread to print 30,000 ’x’s.  */
    thread1_args.character = ’x’;
    thread1_args.count = 30000;
    pthread_create (&thread1_id, NULL, &char_print, &thread1_args);
    /* Create a new thread to print 20,000 o’s.  */
    thread2_args.character = ’o’;
    thread2_args.count = 20000;
    pthread_create (&thread2_id, NULL, &char_print, &thread2_args);
    return 0;
}
搜索更多相关主题的帖子: character void include cookie number 
2012-01-10 21:28
zaixuexi
Rank: 12Rank: 12Rank: 12
来 自:上海
等 级:火箭侠
威 望:8
帖 子:858
专家分:3233
注 册:2010-12-1
得分:0 
程序代码:
#ifdef _WIN32
#include <windows.h>
#include <process.h>
typedef void(*FUNC)(void*);
#else
#include <pthread.h>
#endif
#include <stdio.h>

/* Parameters to print_function.  */
struct char_print_parms
{
    /* The character to print.  */
    char character;
    /* The number of times to print it.  */
    int count;
};
/* Prints a number of characters to stderr, as given by PARAMETERS,
which is a pointer to a struct char_print_parms.  */
void* char_print (void* parameters)
{
    /* Cast the cookie pointer to the right type.  */
    struct char_print_parms* p = (struct char_print_parms*) parameters;
    int i;
    for (i = 0; i < p->count; ++i)
        fputc (p->character, stderr);
    return 0;
}
/* The main program.  */
int main ()
{
#ifndef _WIN32
    pthread_t thread1_id;
    pthread_t thread2_id;
#endif
    struct char_print_parms thread1_args;
    struct char_print_parms thread2_args;
    /* Create a new thread to print 30,000 ’x’s.  */
    thread1_args.character = 'x';
    thread1_args.count = 30000;
#ifdef _WIN32
    _beginthread((FUNC)char_print, 0, &thread1_args);
#else
    pthread_create (&thread1_id, NULL, &char_print, &thread1_args);
#endif
    /* Create a new thread to print 20,000 o’s.  */
    thread2_args.character = 'o';
    thread2_args.count = 20000;
#ifdef _WIN32
    _beginthread((FUNC)char_print, 0, &thread2_args);   
    Sleep(100);
#else
    pthread_create (&thread2_id, NULL, &char_print, &thread2_args);
    usleep(100);
#endif
    return 0;
} 
代码写干净点,不是所有人都跑的posix规范

技术问题,请不要以短消息方式提问
2012-01-10 22:25
madfrogme
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
得分:0 
回复 2楼 zaixuexi
楼主把这贴删了吧,一开始不知道怎么发,

The quieter you become, the more you can hear
2012-01-10 22:46



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-359486-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.314937 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved