C语言多线程的 *** 作步骤

C语言多线程的 *** 作步骤,第1张

线程创建

函数原型:intpthread_create(pthread_trestrict tidp,const pthread_attr_t restrict attr,void (start_rtn)(void),void restrict arg);

返回值:若是成功建立线程返回0,否则返回错误的编号。

形式参数:pthread_trestrict tidp要创建的线程的线程id指针;const pthread_attr_t restrict attr创建线程时的线程属性;void (start_rtn)(void)返回值是void类型的指针函数;void restrict arg start_rtn的形参。

线程挂起:该函数的作用使得当前线程挂起,等待另一个线程返回才继续执行。也就是说当程序运行到这个地方时,程序会先停止,然后等线程id为thread的这个线程返回,然后程序才会断续执行。

函数原型:intpthread_join(pthread_tthread, void value_ptr);

参数说明如下:thread等待退出线程的线程号;value_ptr退出线程的返回值。

返回值:若成功,则返回0;若失败,则返回错误号。

线程退出

函数原型:voidpthread_exit(void rval_ptr);

获取当前线程id

函数原型:pthread_tpthread_self(void);

互斥锁

创建pthread_mutex_init;销毁pthread_mutex_destroy;加锁pthread_mutex_lock;解锁pthread_mutex_unlock。

条件锁

创建pthread_cond_init;销毁pthread_cond_destroy;触发pthread_cond_signal;广播pthread_cond_broadcast;等待pthread_cond_wait。

3个线程使用的都是同一个info

代码 Info_t info = (Info_t )malloc(sizeof(Info_t));只创建了一个info

pthread_create(&threads[i],NULL,calMatrix,(void )info); 三个线程使用的是同一个

我把你的代码改了下:

#include <stdioh>

#include <stdlibh>

#include <pthreadh>

int mtc[3] = { 0 }; // result matrix

typedef struct

{

    int prank;

    int mta; 

    int mtb;

}Info_t;

void calMatrix(void arg)

{

    int i;

    Info_t info = (Info_t )arg;

    int prank = info->prank;

    fprintf(stdout,"calMatrix : prank is %d\n",prank);

    for(i = 0; i < 3; i++)

        mtc[prank] += info->mta[i]  info->mtb[i];

    return NULL;

}

int main(int argc,char argv)

{

    int i,j,k = 0;

    int mta[3][3];

    int mtb[3] = { 1 };

    Info_t info = (Info_t )malloc(sizeof(Info_t)3);

    for(i = 0; i < 3; i++)

        for(j = 0; j < 3; j++)

            mta[i][j] = k++;

    / 3 threads /

    pthread_t threads = (pthread_t )malloc(sizeof(pthread_t)3);

    fprintf(stdout,"\n");fflush(stdout);

    for(i = 0; i < 3; i++)

    {

        info[i]prank = i;

        info[i]mta = mta[i];

        info[i]mtb = mtb;

        pthread_create(&threads[i],NULL,calMatrix,(void )(&info[i]));

    }

    for(i = 0; i < 3; i++)

        pthread_join(threads[i],NULL);

    fprintf(stdout,"\n==== the matrix result ====\n\n");

    fflush(stdout);

    for(i = 0; i < 3; i++)

    {

        fprintf(stdout,"mtc[%d] = %d\n",i,mtc[i]);

        fflush(stdout);

    }

    return 0;

}

矩阵的计算我忘记了,你运行看看结果对不对

// producer_consumercpp

//////////////////////////////////////////////////////////////////////

// 有一个生产者在生产产品,这些产品将提供给若干个消费者去消费,为了使生产者和消费者能并发执行,

// 在两者之间设置一个有多个缓冲区的缓冲池,生产者将它生产的产品放入一个缓冲区中,消费者可以从缓

// 冲区中取走产品进行消费,所有生产者和消费者都是异步方式运行的,但它们必须保持同步,即不允许消

// 费者到一个空的缓冲区中取产品,也不允许生产者向一个已经装满产品且尚未被取走的缓冲区中投放产品。

//////////////////////////////////////////////////////////////////////

#include

#include

#include

#include

const int BUFFER_LENGTH = 100;

int buffer[BUFFER_LENGTH];

int front = 0, rear = -1; // 缓冲区的前端和尾端

int size = 0;

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void producer(void arg);

void consumer(void arg);

int main(int argc, char argv)

{

pthread_t producer_id;

pthread_t consumer_id;

pthread_create(&producer_id, NULL, producer, NULL);

pthread_create(&consumer_id, NULL, consumer, NULL);

//主线程运行结束,子线程也就运行结束了

//

sleep(1);

return 0;

}

void producer(void arg)

{

//pthread_detach(threadid)函数的功能是使线程ID为threadid的线程处于分离状态,一旦线程处于分离状态,

//该线程终止时底 层资源立即被回收;否则终止子线程的状态会一直保存(占用系统资源)直到主线程调用pthread_join(threadid,NULL)获取线程的退 出状态。

//通常是主线程使用pthread_create()创建子线程以后,一般可以调用pthread_detach(threadid)分离刚刚创建的子线程,

//这里的threadid是指子线程的threadid;如此以来,该子线程止时底层资源立即被回收;

//被创建的子线程也可以自己分离自己,子线程调用pthread_detach(pthread_self())就是分离自己,

//因为pthread_self()这个函数返回的就是自己本身的线程ID;

pthread_detach(pthread_self());

while (true)

{

pthread_mutex_lock(&mutex);

while (size == BUFFER_LENGTH) // 如果缓冲区已满,等待; 否则,添加新产品

{

printf("buffer is full producer is waiting\n");

pthread_cond_wait(&cond, &mutex);

}

// 往尾端添加一个产品

rear = (rear + 1) % BUFFER_LENGTH;

buffer[rear] = rand() % BUFFER_LENGTH;

printf("producer produces the item %d: %d\n", rear, buffer[rear]);

++size;

if (size == 1) // 如果当前size=1, 说明以前size=0, 消费者在等待,则给消费者发信号

{

pthread_cond_signal(&cond);

}

pthread_mutex_unlock(&mutex);

}

}

void consumer(void arg)

{

pthread_detach(pthread_self());

while (true)

{

pthread_mutex_lock(&mutex);

while(size == 0) // 如果缓冲区已空,等待; 否则,消费产品

{

printf("buffer is empty consumer is waiting\n");

pthread_cond_wait(&cond, &mutex);

}

// 从前端消费一个产品

printf("consumer consumes an item%d: %d\n", front, buffer[front]);

front = (front + 1) % BUFFER_LENGTH;

--size;

if (size == BUFFER_LENGTH-1) // 如果当前size=BUFFER_LENGTH-1,说明以前生产者在等待,则给生产者发信号

{

pthread_cond_signal(&cond);

}

pthread_mutex_unlock(&mutex);

}

}

以上就是关于C语言多线程的 *** 作步骤全部的内容,包括:C语言多线程的 *** 作步骤、linux系统下,c语言pthread多线程编程传参问题、怎样用C++实现生产者消费者的模拟等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9675102.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-30
下一篇2023-04-30

发表评论

登录后才能评论

评论列表(0条)

    保存