当前位置:首页 » 操作系统 » linux的多线程

linux的多线程

发布时间: 2022-02-16 02:21:33

java的多线程与linux的多线程的关系

java自己实现了线程库,也就是说java的线程并不和操作系统的线程对应,jvm在操作系统上面是一个进程,当这个进程被操作系统调度到后,jvm内部实现的线程库再调度java线程,为什么是这样呢?考虑到以前的操作系统内核,比如linux,在以前都不直接支持线程,用户线程和内核线程是多对一的关系,solaris一度也是这样,所以java当然心有余而力不足了,你操作系统都不能完美支持线程,你让我实现不是难为我吗?在那个年代,java多线程的调度完全是自主的,操作系统根本不知道java是多线程的,调度策略完全自己实现,单cpu下肯定是分时的,多cpu下就看jvm会不会建立多cpu上的多jvm实例了。
到了后来,操作系统内核纷纷都支持了多线程(windows开始就支持),那么java也要考虑推卸一些责任了,这样java线程就和操作系统线程一一对应或多多对应了,这个时候,如果是一一对应,那么线程的调度完全交给了操作系统内核,当然jvm还保留一些策略足以影响到其内部的线程调度,举个例子,在linux下,只要一个Thread.run就会调用一个fork产生一个线程。
问:java获得cup使用权采用的抢占机制,使用cup的时候是分时机制,这句话对不对?
答:部分对,早期实现,基本可以实现抢占式,但是现代实现,如果系统不支持抢占,那么jvm也无所谓抢占了。
问:多线程使用cup和使用的操作系统有关还是java机制有关(xp是什么机制)
答:早期是java机制实现,现在大部分是操作系统实现的,java机制仅仅保留了相关策略从而影响调度;xp是基于优先级的抢占式调度,其性能很大程度依赖于动态优先级提升

⑵ linux多线程问题

这应该是生产者与消费者的模型。。

队列操作的函数+mutex 互斥锁

然后用阻塞方式获取锁就OK拉。。

⑶ linux多线程问题。

少了pthread_t t1,t2吧。

gcc file.c -o file -lpthread编译. 运行结果:

线程--1
线程--2
线程--2
信号!

可以收到信号。你有什么问题?

⑷ linux下多线程同步

你的问题不是互斥的问题,而是传给子线程的 i 是指针,在子线程获取 *arg 时,主线程的 for 循环可能已经修改或者没有修改 i 的值,从而出现问题。下面的代码直接把 i 的值传给子线程,而不是传指针,就不会有问题了。


#include<iostream>
usingnamespacestd;
#include<pthread.h>
#include<semaphore.h>
sem_tg_sem;
pthread_mutex_tg_mutex;
staticintg_count=0;
constintTHREAD_NUM=100;
void*ProcThread(void*arg)
{
longiNum=(long)arg;
sem_post(&g_sem);
pthread_mutex_lock(&g_mutex);
sleep(2);
g_count=g_count+1;
cout<<"childthreadnum="<<iNum<<",count="<<g_count<<endl;
pthread_mutex_unlock(&g_mutex);
pthread_exit(NULL);
returnNULL;
}
intmain(){
sem_init(&g_sem,0,1);
pthread_mutex_init(&g_mutex,NULL);
pthread_tchildThread[THREAD_NUM];
for(inti=0;i<THREAD_NUM;++i)
{
sem_wait(&g_sem);
intiRet=pthread_create(childThread+i,NULL,ProcThread,(void*)i);
if(iRet!=0)
{
cout<<"error="<<iRet<<endl;
}
}
for(inti=0;i<THREAD_NUM;++i)
{
pthread_join(childThread[i],NULL);
}
sem_destroy(&g_sem);
pthread_mutex_destroy(&g_mutex);
cout<<"!!!HelloWorld!!!"<<endl;//prints!!!HelloWorld!!!
return0;
}

⑸ Linux多线程编程,替代sleep的几种方式

我只想要进程的某个线程休眠一段时间的,可是用sleep()是将整个进程都休眠的,这个可能就达不到,我们想要的效果了。 目前我知道有三种方式:
1 usleep
这个是轻量级的, 听说能可一实现线程休眠, 我个人并不喜欢这种方式,所以我没有验证它的可行信(个人不推荐)。
2 select
这个可以,我也用过这种方式, 它是在轮询。
3 pthread_cond_timedwait

采用pthread_cond_timedwait(pthread_cond_t* cond, pthread_mutex_t *mutex, const struct timespec *abstime)可以优雅的解决该问题,设置等待条件变量cond,如果超时,则返回;如果等待到条件变量cond,也返回。本文暂不将内部机理,仅演示一个demo。
首先,看这段代码,thr_fn为一个线程函数:
#include <stdio.h>#include <stdlib.h>int flag = 1;void * thr_fn(void * arg) {while (flag){printf("******\n");sleep(10);}printf("sleep test thread exit\n");}int main() {pthread_t thread;if (0 != pthread_create(&thread, NULL, thr_fn, NULL)) {printf("error when create pthread,%d\n", errno);return 1;}char c ;while ((c = getchar()) != 'q');printf("Now terminate the thread!\n");flag = 0;printf("Wait for thread to exit\n");pthread_join(thread, NULL);printf("Bye\n");return 0;}

输入q后,需要等线程从sleep中醒来(由挂起状态变为运行状态),即最坏情况要等10s,线程才会被join。采用sleep的缺点:不能及时唤醒线程。
采用pthread_cond_timedwait函数实现的如下:
#include <stdio.h>#include <sys/time.h>#include <unistd.h>#include <pthread.h>#include <errno.h>static pthread_t thread;static pthread_cond_t cond;static pthread_mutex_t mutex;static int flag = 1;void * thr_fn(void * arg) {struct timeval now;struct timespec outtime;pthread_mutex_lock(&mutex);while (flag) {printf("*****\n");gettimeofday(&now, NULL);outtime.tv_sec = now.tv_sec + 5;outtime.tv_nsec = now.tv_usec * 1000;pthread_cond_timedwait(&cond, &mutex, &outtime);}pthread_mutex_unlock(&mutex);printf("cond thread exit\n");}int main(void) {pthread_mutex_init(&mutex, NULL);pthread_cond_init(&cond, NULL);if (0 != pthread_create(&thread, NULL, thr_fn, NULL)) {printf("error when create pthread,%d\n", errno);return 1;}char c ;while ((c = getchar()) != 'q');printf("Now terminate the thread!\n");pthread_mutex_lock(&mutex);flag = 0;pthread_cond_signal(&cond);pthread_mutex_unlock(&mutex);printf("Wait for thread to exit\n");pthread_join(thread, NULL);printf("Bye\n");return 0;}

pthread_cond_timedwait()函数阻塞住调用该函数的线程,等待由cond指定的条件被触发(pthread_cond_broadcast() or pthread_cond_signal())。
当pthread_cond_timedwait()被调用时,调用线程必须已经锁住了mutex。函数pthread_cond_timedwait()会对mutex进行【解锁和执行对条件的等待】(原子操作)。这里的原子意味着:解锁和执行条件的等待是原则的,一体的。(In this case, atomically means with respect to the mutex andthe condition variable and other access by threads to those objectsthrough the pthread condition variable interfaces.)
如果等待条件满足或超时,或线程被取消,调用线程需要在线程继续执行前先自动锁住mutex,如果没有锁住mutex,产生EPERM错误。即,该函数返回时,mutex已经被调用线程锁住。
等待的时间通过abstime参数(绝对系统时间,过了该时刻就超时)指定,超时则返回ETIMEDOUT错误码。开始等待后,等待时间不受系统时钟改变的影响。
尽管时间通过秒和纳秒指定,系统时间是毫秒粒度的。需要根据调度和优先级原因,设置的时间长度应该比预想的时间要多或者少点。可以通过使用系统时钟接口gettimeofday()获得timeval结构体。

⑹ linux如何实现多线程

#/bin/bashall_num=10a=$(date +%H%M%S)for num in `seq 1 ${all_num}`do
sleep 1
echo ${num}
done

b=$(date +%H%M%S)

echo -e "startTime:\t$a"echo -e "endTime:\t$b"

⑺ linux下的多线程

thread_return指向某存储线程返回值的变量。
倘若线程返回值是一个字符串。我们当然可以用一个指针void *thread_return 搞定。
但如果有多个返回值或者返回的是一个结构体,那么void *thread_return就不能满足需要了。所以需要用void **thread_return。
供参考,非权威解释。

⑻ 关于linux下多线程编程

pthread_join 线程停止等待函数没有调用

pthread_create 线程生成后,没有等子线程停止,主线程就先停止了。

主线程停止后,整个程序停止,子线程在没有printf的时候就被结束了。

结论:不是你没有看到结果,而是在子线程printf("..................\n");之前整个程序就已经停止了。

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>

#define FALSE -1
#define TRUE 0

void *shuchu( void *my )
{
int j;
printf("..................\n");
}
int main()
{
int i = 0;
int rc = 0;
int ret1;
pthread_t p_thread1;
if(0!=(ret1 = pthread_create(&p_thread1, NULL, shuchu, NULL)))printf("sfdfsdfi\n");
printf("[%d]\n",p_thread1);
pthread_join(p_thread1, NULL);
return TRUE;

}

⑼ linux线程如何运行

pthread_create执行后,如果执行成功会生成一个子线程 也就是现在有两个线程同时运行
父线程还会继续执行后面的代码 直到结束
子线程则开始执行thread函数体里的代码了 别的不执行
pthread_join会按照父线程执行顺序 到它了就会执行 该函数的作用是阻塞等待一个线程执行完毕
在你的代码里 不一定在子线程执行3次后才启动 也可能子线程没有执行呢 父线程就执行到pthread_join了 然后阻塞等待子线程
如果你想让pthread_join在子线程3次执行后才启动 可以让父线程sleep下 不过子线程执行完了 你再执行pthread_join也就没有什么意义了
不懂再问

⑽ Linux下多线程的如何执行

主线程结束,则进程结束,属于该进程的所有线程都会结束,可以在主线程中join,也可以在主线程中加死循环。

热点内容
python3默认安装路径 发布:2024-09-19 08:50:22 浏览:514
环卫视频拍摄脚本 发布:2024-09-19 08:35:44 浏览:416
sqlserveronlinux 发布:2024-09-19 08:16:54 浏览:255
编程常数 发布:2024-09-19 08:06:36 浏览:951
甘肃高性能边缘计算服务器云空间 发布:2024-09-19 08:06:26 浏览:161
win7家庭版ftp 发布:2024-09-19 07:59:06 浏览:716
数据库的优化都有哪些方法 发布:2024-09-19 07:44:43 浏览:268
知乎华为编译器有用吗 发布:2024-09-19 07:32:20 浏览:617
访问虚拟机磁盘 发布:2024-09-19 07:28:13 浏览:668
原地工作算法 发布:2024-09-19 07:28:07 浏览:423