C 使用互斥锁按顺序同步线程
我有一个C程序,它必须以0-6的顺序显示线程。我使用互斥体,但当我尝试运行代码时,什么都没有发生,什么都没有显示。而且,编译器没有显示任何错误 我使用了锁定和解锁互斥,但我不确定我是否在正确的位置创建了它。 感谢您的建议和帮助C 使用互斥锁按顺序同步线程,c,multithreading,synchronization,pthreads,mutex,C,Multithreading,Synchronization,Pthreads,Mutex,我有一个C程序,它必须以0-6的顺序显示线程。我使用互斥体,但当我尝试运行代码时,什么都没有发生,什么都没有显示。而且,编译器没有显示任何错误 我使用了锁定和解锁互斥,但我不确定我是否在正确的位置创建了它。 感谢您的建议和帮助 #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <pthread.h> #include <time.h> void *
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <time.h>
void *text(void *arg);
long code[] = { 4, 6, 3, 1, 5, 0, 2 }; // Order in which to start threads
int num = 0;
pthread_mutex_t a_mutex = PTHREAD_MUTEX_INITIALIZER;
int main()
{
int i;
pthread_t tid[7];
// Initialize random number generator
time_t seconds;
time(&seconds);
srand((unsigned int) seconds);
int rc;
// Create our threads
for (i = 0; i < 7; i++)
{
pthread_create(&tid[i], NULL, text, (void*)code[i]);
for (i = 0; i < 7; i++)
{ rc = pthread_mutex_lock(&a_mutex);
for (i = 0; i < 7; i++)
{
rc = pthread_mutex_unlock(&a_mutex);
}
}
}
//join threads
for (i=0; i<7; i++)
{
if (pthread_join(tid[i], NULL));
}
rc = pthread_mutex_destroy(&a_mutex);
// Exit main
return 0;
}
void *text(void *arg)
{
long n = (long)arg;
int rand_sec = rand() % (3 - 1 + 1) + 1; // Random num seconds to sleep
while (num != n) {} // Busy wait used to wait for our turn
num++; // Let next thread go
sleep(rand_sec); // Sleep for random amount of time
printf("This is thread %d.\n", n);
// Exit thread
pthread_exit(0);
}
#包括
#包括
#包括
#包括
#包括
void*文本(void*arg);
长代码[]={4,6,3,1,5,0,2};//启动线程的顺序
int num=0;
pthread\u mutex\u t a\u mutex=pthread\u mutex\u初始值设定项;
int main()
{
int i;
pthread_t tid[7];
//初始化随机数生成器
时间t秒;
时间(秒);
srand((无符号整数)秒);
int rc;
//创建我们的线程
对于(i=0;i<7;i++)
{
pthread_创建(&tid[i],NULL,text,(void*)代码[i]);
对于(i=0;i<7;i++)
{rc=pthread_mutex_lock(&a_mutex);
对于(i=0;i<7;i++)
{
rc=pthread\u mutex\u unlock(&a\u mutex);
}
}
}
//连接线程
对于(i=0;i解决问题的关键是问自己“我的6个线程之间共享的数据段是什么?”这是需要互斥保护的变量(从中读取和写入)在锁定的互斥体块中。当前,您仅从单个主线程锁定和解锁互斥体,实际上什么都不做
您可能想要更接近这一点(尽管这可以大大简化-例如,您可以完全取消睡眠):
#包括
#包括
#包括
#包括
#包括
void*文本(void*arg);
长代码[]={4,6,3,1,5,0,2};//启动线程的顺序
int num=0;
pthread\u mutex\u t a\u mutex=pthread\u mutex\u初始值设定项;
int main()
{
int i;
pthread_t tid[7];
//初始化随机数生成器
时间t秒;
时间(秒);
srand((无符号整数)秒);
//创建我们的线程
对于(i=0;i<7;i++)
pthread_创建(&tid[i],NULL,text,(void*)代码[i]);
//连接线程
对于(i=0;i这里有一个最小版本的代码,删除了等待和随机部分,因为它们转移了人们对锁工作方式的注意力
线程自动排队等待互斥锁
您需要请求例程函数中的锁
始终记住关闭线程并释放所有可能代码路径上的锁
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <time.h>
void *text(void *arg);
long code[] = { 4, 6, 3, 1, 5, 0, 2 }; // Order in which to start threads
int num = 0;
pthread_mutex_t a_mutex = PTHREAD_MUTEX_INITIALIZER;
int main()
{
int i;
pthread_t tid[7];
// Create our threads
for (i = 0; i < 7; i++)
{
pthread_create( &tid[i], NULL, text, (void*) & code[i] );
}
//join threads
for (i=0; i<7; i++)
{
pthread_join(tid[i], NULL);
}
pthread_mutex_destroy( & a_mutex );
// Exit main
return 0;
}
void *text(void *arg)
{
while(1){
pthread_mutex_lock(&a_mutex);
if (num == *(int *) arg){
printf("This is thread has the code %d\n",*(int *) arg);
num++;
pthread_mutex_unlock(&a_mutex);
pthread_exit(0);
}
pthread_mutex_unlock(&a_mutex);
}
}
#包括
#包括
#包括
#包括
#包括
void*文本(void*arg);
长代码[]={4,6,3,1,5,0,2};//启动线程的顺序
int num=0;
pthread\u mutex\u t a\u mutex=pthread\u mutex\u初始值设定项;
int main()
{
int i;
pthread_t tid[7];
//创建我们的线程
对于(i=0;i<7;i++)
{
pthread_创建(&tid[i],NULL,text,(void*)和code[i]);
}
//连接线程
对于(i=0;i@Anastasia Netz。您需要仅在代码的关键部分而不仅仅在主线程中使用pthread\u mutex\u lock和pthread\u mutex\u unlock
mutex。请确定您的关键部分(部分,其中完成了一项重要的工作,并且是所有执行线程所共有的,但只有其中一个线程可以访问该部分),请在代码中小心地使用这些互斥体。
现在让我解释一下您的代码:
在您的代码中,您只需创建七个线程。在创建每个线程后,您只需锁定互斥锁一次,然后无缘无故地将其解锁七次。因此,在完成所有七次迭代后(for
,创建这七个线程),您已锁定互斥锁七次,并将其解锁七次(7*7=49)四十九次。
以下是执行此任务的代码:
(i=0;i<7;i++)的
pthread_创建(&tid[i],NULL,text,(void*)代码[i]);
(i=0;i<7;i++)的
{
rc=pthread\u mutex\u lock(&a\u mutex);
对于(i=0;i<7;i++)
{
rc=pthread\u mutex\u unlock(&a\u mutex);
}
}}
请查看Xvan的代码
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <time.h>
void *text(void *arg);
long code[] = { 4, 6, 3, 1, 5, 0, 2 }; // Order in which to start threads
int num = 0;
pthread_mutex_t a_mutex = PTHREAD_MUTEX_INITIALIZER;
int main()
{
int i;
pthread_t tid[7];
// Create our threads
for (i = 0; i < 7; i++)
{
pthread_create( &tid[i], NULL, text, (void*) & code[i] );
}
//join threads
for (i=0; i<7; i++)
{
pthread_join(tid[i], NULL);
}
pthread_mutex_destroy( & a_mutex );
// Exit main
return 0;
}
void *text(void *arg)
{
while(1){
pthread_mutex_lock(&a_mutex);
if (num == *(int *) arg){
printf("This is thread has the code %d\n",*(int *) arg);
num++;
pthread_mutex_unlock(&a_mutex);
pthread_exit(0);
}
pthread_mutex_unlock(&a_mutex);
}
}
for (i = 0; i < 7; i++)
{
rc = pthread_mutex_lock(&a_mutex);
for (i = 0; i < 7; i++)
{
rc = pthread_mutex_unlock(&a_mutex);
}
}}