⑴ c语言多个线程循环次数极大时如何减小时间差实现同步运行
使用线程同步技术,可以协调多线程的同步问题
⑵ 用C语言开多线程,想让多个相同的子线程同时运行,怎么实现
工作线程是处理后台工作的,创建一个线程非常简单,只需要两步:实线线程函数和开始线程.不需要由CWinThread派生类,你可以不加修改地使用CWinThread。
AfxBeginThread有两种形式,一种是用来创建用户界面线程的,另一种就是用来创建工作线程的.为了开始执行线程,只需要向AfxBeginThread提供下面的参数就可以了.
1.线程函数的地址
2.传送到线程函数的参数
3.(可选的)线程的优先级,可参阅::SetThreadPriority
4.(可选的)线程开始时候的状态,可设置为CREATE_SUSPENEDE
5.(可选的)线程的安全属性,请参阅SECURITY_ATTRIBUTES
实例代码
UINT ThreadProc(LPVOID pParam)
{
return 0;//线程成功完成
}
CWinThread* AfxBeginThreadProc,//线程函数地址
LPVOID pParam,//线程参数
int nPriority=THREAD+PRIORITY_NOMAL,//线程优先级
int nStackSize=0,//线程堆栈大小,默认为1M
DWORD dwCreateFlags=0,
LPSECURITY_ATTRIBUTES lpSecurityAttrs=NULL
);
⑶ 如何用c语言编写同步与互斥线程 csdn
pthread_create(pthread_t *thread, pthread_attr_t * attr, void *(*start_routine)(void *),void *arg);创建线程(默认为同步线程)
线程的互斥函数有:互斥函数的初始化pthread_mutex_init(),互斥函数的锁定函数pthread_mutex_lock(),互斥函数的预锁定函数pthread_mutex_trylock(),互斥函数的解锁函数pthread_mutex_unlock(),互斥函数的销毁函数pthread_mutex_destroy()
⑷ C语言如何在线程间实现同步和互斥
线程之间的同步和互斥解决的问题是线程对共同资源进行访问。Posix有两种方式:
信号量和互斥锁;信号量适用同时可用的资源为多个的情况;互斥锁适用于线程可用的资源只有一个的情况
1、互斥锁:互斥锁是用加锁的方式来控制对公共资源的原子操作(一旦开始进行就不会被打断的操作)
互斥锁只有上锁和解锁两种状态。互斥锁可以看作是特殊意义的全局变量,因为在同一时刻只有一个线程能够对互斥锁进行操作;只有上锁的进程才可以对公共资源进行访问,其他进程只能等到该进程解锁才可以对公共资源进行操作。
互斥锁操作函数:
pthread_mutex_init();//初始化
pthread_mutex_lock();//上锁 参数:pthread_mutex_t *mutex
pthread_mutex_trylock();//判断上锁 参数:pthread_mutex_t *mutex
pthread_mutex_unlock();//解锁 参数:pthread_mutex_t *mutex
pthread_mutex_release();//消除互斥锁 参数:pthread_mutex_t *mutex
互斥锁分为快速互斥锁、递归互斥锁、检错互斥锁;在 init 的时候确定
int pthread_mutex_t(pthread_mutex_t *mutex, const pthread_mutex_t mutexattr);
第一个参数:进行操作的锁
mutexattr:锁的类型,默认快速互斥锁(阻塞)123456789
2、信号量:信号量本质上是一个计数器,在操作系统做用于PV原子操作;
P操作使计数器-1;V操作使计数器+1.
在互斥操作中可以是使用一个信号量;在同步操作中需要使用多个信号量,并设置不同的初始值安排它们顺序执行
sem_init(); // 初始化操作
sem_wait(); // P操作,计数器减一;阻塞 参数:sem_t *sem
sem_trywait(); // P操作,计数器减一;非阻塞 参数:sem_t *sem
sem_post(); // V操作,计数器加一 参数:sem_t *sem
sem_destroy(); // 销毁信号量 参数:sem_t *sem
sem_init(sem_t *sem, int pshared, int value);
pshared用于指定多少个进程共享;value初始值
⑸ c语言线程同步
可以同步的哦!
⑹ c语言实例,linux线程同步的信号量方式 谢谢
这么高的悬赏,实例放后面。信号量(sem),如同进程一样,线程也可以通过信号量来实现通信,虽然是轻量级的。信号量函数的名字都以"sem_"打头。线程使用的基本信号量函数有四个。
信号量初始化。
intsem_init(sem_t*sem,intpshared,unsignedintvalue);
这是对由sem指定的信号量进行初始化,设置好它的共享选项(linux只支持为0,即表示它是当前进程的局部信号量),然后给它一个初始值VALUE。
等待信号量。给信号量减1,然后等待直到信号量的值大于0。
intsem_wait(sem_t*sem);
释放信号量。信号量值加1。并通知其他等待线程。
intsem_post(sem_t*sem);
销毁信号量。我们用完信号量后都它进行清理。归还占有的一切资源。
intsem_destroy(sem_t*sem);
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#include<semaphore.h>
#include<errno.h>
#definereturn_if_fail(p)if((p)==0){printf("[%s]:funcerror!/n",__func__);return;}
typedefstruct_PrivInfo
{
sem_ts1;
sem_ts2;
time_tend_time;
}PrivInfo;
staticvoidinfo_init(PrivInfo*thiz);
staticvoidinfo_destroy(PrivInfo*thiz);
staticvoid*pthread_func_1(PrivInfo*thiz);
staticvoid*pthread_func_2(PrivInfo*thiz);
intmain(intargc,char**argv)
{
pthread_tpt_1=0;
pthread_tpt_2=0;
intret=0;
PrivInfo*thiz=NULL;
thiz=(PrivInfo*)malloc(sizeof(PrivInfo));
if(thiz==NULL)
{
printf("[%s]:Failedtomallocpriv./n");
return-1;
}
info_init(thiz);
ret=pthread_create(&pt_1,NULL,(void*)pthread_func_1,thiz);
if(ret!=0)
{
perror("pthread_1_create:");
}
ret=pthread_create(&pt_2,NULL,(void*)pthread_func_2,thiz);
if(ret!=0)
{
perror("pthread_2_create:");
}
pthread_join(pt_1,NULL);
pthread_join(pt_2,NULL);
info_destroy(thiz);
return0;
}
staticvoidinfo_init(PrivInfo*thiz)
{
return_if_fail(thiz!=NULL);
thiz->end_time=time(NULL)+10;
sem_init(&thiz->s1,0,1);
sem_init(&thiz->s2,0,0);
return;
}
staticvoidinfo_destroy(PrivInfo*thiz)
{
return_if_fail(thiz!=NULL);
sem_destroy(&thiz->s1);
sem_destroy(&thiz->s2);
free(thiz);
thiz=NULL;
return;
}
staticvoid*pthread_func_1(PrivInfo*thiz)
{
return_if_fail(thiz!=NULL);
while(time(NULL)<thiz->end_time)
{
sem_wait(&thiz->s2);
printf("pthread1:pthread1getthelock./n");
sem_post(&thiz->s1);
printf("pthread1:pthread1unlock/n");
sleep(1);
}
return;
}
staticvoid*pthread_func_2(PrivInfo*thiz)
{
return_if_fail(thiz!=NULL);
while(time(NULL)<thiz->end_time)
{
sem_wait(&thiz->s1);
printf("pthread2:pthread2gettheunlock./n");
sem_post(&thiz->s2);
printf("pthread2:pthread2unlock./n");
sleep(1);
}
return;
}
⑺ C语言多线程线程同步可以干什么
线程同步
比如你要操作大量数据,再有界面的情况下会卡住,实际上是在运行的,但是你会以为它卡死了,线程同步就是解决这类问题,让操作量大的工作交给一个线程,界面的显示交给另一个线程