导航:首页 > 机械设备 > 机械手多个线程程序

机械手多个线程程序

发布时间:2021-02-23 18:55:37

⑴ pthread多线程是指同时运行多个程序吗

不是的,复windows每次运行只能运行一个制程序或者一个进程就是process。
进程里面可以调用多个线程thread,多个线程是按照CPU分时间片进行的,所以看上去跟同时运行一样。
由于多个线程是在同一个进程里面运行的,所以线程之间是可以通信的;而线程之间还可能用到同一个进程里面的相同数据。
线程之间的通信请参看参考资料里面的线程同步。

⑵ 如何让多个程序运行在一个进程的多个线程中

如果你的“控制上网”程序打开就能生效,不用每次都设置的话,把程序加到注册回表启答动组里去:

位置:
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run]
在右面板处点右键“新建--》字符串值”,名字随便起,然后双击打开它,把你的程序执行文件的完整路径敲进去,确定即可。

例如:我添加“NET.exe”,那么就打“C:\WINDOWS\system32\ctfmon.exe”,依此类推。

给分哦,呵呵~~
希望对你有帮助

⑶ c语言如何编写一个简单的多线程程序

这是一个多线程例子,里面只有两个线程,是生产者/消费者模式,已编译通过,注释很详细,
如下:

/* 以生产者和消费者模型问题来阐述Linux线程的控制和通信你
生产者线程将生产的产品送入缓冲区,消费者线程则从中取出产品。
缓冲区有N个,是一个环形的缓冲池。
*/
#include <stdio.h>
#include <pthread.h>

#define BUFFER_SIZE 16

struct prodcons
{
int buffer[BUFFER_SIZE];/*实际存放数据的数组*/
pthread_mutex_t lock;/*互斥体lock,用于对缓冲区的互斥操作*/
int readpos,writepos; /*读写指针*/
pthread_cond_t notempty;/*缓冲区非空的条件变量*/
pthread_cond_t notfull;/*缓冲区未满 的条件变量*/
};

/*初始化缓冲区*/
void pthread_init( struct prodcons *p)
{
pthread_mutex_init(&p->lock,NULL);
pthread_cond_init(&p->notempty,NULL);
pthread_cond_init(&p->notfull,NULL);
p->readpos = 0;
p->writepos = 0;
}

/*将产品放入缓冲区,这里是存入一个整数*/
void put(struct prodcons *p,int data)
{
pthread_mutex_lock(&p->lock);
/*等待缓冲区未满*/
if((p->writepos +1)%BUFFER_SIZE ==p->readpos)
{
pthread_cond_wait(&p->notfull,&p->lock);
}
p->buffer[p->writepos] =data;
p->writepos++;
if(p->writepos >= BUFFER_SIZE)
p->writepos = 0;
pthread_cond_signal(&p->notempty);
pthread_mutex_unlock(&p->lock);
}
/*从缓冲区取出整数*/
int get(struct prodcons *p)
{
int data;
pthread_mutex_lock(&p->lock);
/*等待缓冲区非空*/
if(p->writepos == p->readpos)
{
pthread_cond_wait(&p->notempty ,&p->lock);//非空就设置条件变量notempty
}
/*读书据,移动读指针*/
data = p->buffer[p->readpos];
p->readpos++;
if(p->readpos == BUFFER_SIZE)
p->readpos = 0;
/*设置缓冲区未满的条件变量*/
pthread_cond_signal(&p->notfull);
pthread_mutex_unlock(&p->lock);
return data;
}
/*测试:生产站线程将1 到1000的整数送入缓冲区,消费者线程从缓冲区中获取整数,两者都打印信息*/
#define OVER (-1)
struct prodcons buffer;
void *procer(void *data)
{
int n;
for( n=0;n<1000;n++)
{
printf("%d ------>\n",n);
put(&buffer,n);
}
put(&buffer,OVER);
return NULL;
}
void *consumer(void *data)
{
int d;
while(1)
{
d = get(&buffer);
if(d == OVER)
break;
else
printf("----->%d\n",d);
}
return NULL;
}
int main()
{
pthread_t th_p,th_c;
void *retval;
pthread_init(&buffer);
pthread_create(&th_p,NULL,procer,0);
pthread_create(&th_c,NULL,consumer,0);
/*等待两个线程结束*/
pthread_join(th_p, &retval);
pthread_join(th_c,&retval);
return 0;
}

⑷ 怎么才能得实现多线程并发,在一个程序中有多个线程同时执行~

DWORD WINAPI ThreadProc1( LPVOID lpvUser )
{

for( int i = 0; i < 5; i++ )
{
Sleep( 1 );//added.
cout<<"1"<<endl;
}

return 0;
}

DWORD WINAPI ThreadProc( LPVOID lpvUser )
{

for( int i = 0; i < 5; i++ )
{
Sleep( 1 );//added.
cout<<"2"<<endl;
}

return 0;
}

说实话,实现这个是没有意义的,多线程的并发主要是通过同步手段来实现的。同步的内核对象例如,临界区、信号量和互斥量,等。一般是用来对共享资源的保护。所以,干这种事没有意义,可能您想模拟时间片轮询,当然可以,对于线程默认创建的优先级都是一样的,所以他们确实是按照时间片轮询的方式调度的。为什么,不能输出你想要的结果呢?是因为一个时间片长足以让你的语句输出多次了。

⑸ 编写一个多线程的java 程序

public class RunThread implements Runnable{
String name;
Thread runner;
static boolean bool=true;
public RunThread(String threadName) {
name=threadName;
}
public void onStart()
{
runner = new Thread(this);
runner.setName(name);
runner.start();
}
public void run() {
String name=Thread.currentThread().getName();
System.out.println(name + " 线程运行开始!");
int index=0;
if(name.equals("小写字母"))
index='a';
else if(name.equals("大写字母"))
index='A';
while(!bool);
bool=false;
for(int i=index;i<26+index;i++)
System.out.print((char)i+" ");
System.out.println();
bool=true;
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name + " 线程运行结束!");
}
public static void main(String[] args) {
RunThread a=new RunThread("小写字母");
RunThread b=new RunThread("大写字母");
a.onStart();
b.onStart();
//System.out.println(" 线程运行开始!");
}

⑹ 编写多线程程序有几种实现方式

有三种:
(1)继承Thread类,重写run函数
创建:
class xx extends Thread{
public void run(){
Thread.sleep(1000) //线程休眠1000毫秒,sleep使线程进入Block状态,并释放资源
}}
开启线程:
对象.start() //启动线程,run函数运行
(2)实现Runnable接口,重写run函数
开启线程:
Thread t = new Thread(对象) //创建线程对象
t.start()
(3)实现Callable接口,重写call函数
Callable是类似于Runnable的接口,实现Callable接口的类和实现Runnable的类都是可被其它线程执行的任务。
Callable和Runnable有几点不同:
①Callable规定的方法是call(),而Runnable规定的方法是run().
②Callable的任务执行后可返回值,而Runnable的任务是不能返回值的
③call()方法可抛出异常,而run()方法是不能抛出异常的。
④运行Callable任务可拿到一个Future对象,Future表示异步计算的结果。它提供了检查计算是否完成的方法,以等
待计算的完成,并检索计算的结果.通过Future对象可了解任务执行情况,可取消任务的执行,还可获取任务执行的结果

⑺ 编写一个多线程程序,使得3个线程同时运行。

才给5分啊~~~~~~难怪大家懒得做。我就贴一下读写者问题的多线程程序吧,希望对你有帮助。(好像是2个线程,太久了懒得看)
#include "windows.h"
#include <conio.h>
#include <stdlib.h>
#include <fstream.h>
#include <io.h>
#include <string.h>
#include <stdio.h>

#define READER 'R' //读者
#define WRITER 'W' //写者
#define INTE_PER_SEC 1000 //每秒时钟中断次数
#define MAX_THREAD_NUM 64 //最大线程数目
#define MAX_FILE_NUM 32 //最大数据文件数目
#define MAX_STR_LEN 32 //字符串长度

int readcount=0; //读者数目 (属临界资源,需以互斥方式访问)
int writecount=0; //写者数目 (属临界资源,需以互斥方式访问)
//定义3个临界资源

CRITICAL_SECTION RP_Write; //临界区控制变量 (用于读者优先)
CRITICAL_SECTION cs_Write; //临界区控制变量 (用于写者优先)
CRITICAL_SECTION cs_Read; //临界区控制变量 (用于写者优先)
//定义线程及其操作行为信息结构

struct ThreadInfo
{
int serial; //线程序号
char entity; //线程类别(是读者还是写者)
double delay; //线程延迟
double persist; //线程读写操作的持续时间
};

// 读者优先――读者线程
void RP_ReaderThread(void *p) //p: 读者线程信息
{
//互斥变量
HANDLE h_Mutex;
h_Mutex = OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex_for_readcount");
//从参数中获得信息
DWORD wait_for_mutex; //等待互斥变量所有权
DWORD m_delay; //延迟时间
DWORD m_persist; //读文件持续时间
int m_serial; //线程序号

m_serial = ((ThreadInfo*)(p))->serial;
m_delay = (DWORD)((ThreadInfo*)(p))->delay*INTE_PER_SEC;
m_persist = (DWORD)((ThreadInfo*)(p))->persist*INTE_PER_SEC;
Sleep(m_delay); //延迟等待
//输出读进程已发出读请求的提示信息
printf ("Reader thread %d sends the reading require.\n", m_serial);

//等待互斥信号,保证对readcount的互斥访问
wait_for_mutex = WaitForSingleObject(h_Mutex,-1);
readcount++;
if (readcount==1) //第一个读者,等待临界区资源
EnterCriticalSection(&RP_Write);
ReleaseMutex(h_Mutex); //释放互斥信号
//输出读进程I 正在临界区中读操作的提示信息
printf("Reader thread %d begins to read file.\n", m_serial);
Sleep(m_persist);
//退出线程
printf("Reader thread %d finished reading file. \n",m_serial);
//等待互斥信号,保证对readcount的互斥访问
wait_for_mutex = WaitForSingleObject(h_Mutex,-1);
readcount--;
if (readcount==0) LeaveCriticalSection(&RP_Write);
ReleaseMutex(h_Mutex); //释放互斥信号
}

// 读者优先――写者线程
void RP_WriterThread(void *p) //p: 写者线程信息
{
DWORD m_delay; //延迟时间
DWORD m_persist; //读文件持续时间
int m_serial; //线程序号
//从参数中获取信息m_serial, m_delay, m_persist ,
m_serial = ((ThreadInfo*)(p))->serial;
m_delay = (DWORD)((ThreadInfo*)(p))->delay*INTE_PER_SEC;
m_persist = (DWORD)((ThreadInfo*)(p))->persist*INTE_PER_SEC;

//延迟等待m_delay秒;
Sleep(m_delay);
//输出信息"写线程 %d 发出写请求;
printf ("Writer thread %d sends the writing require.\n", m_serial);
//等待临界资源
EnterCriticalSection(&RP_Write);
//输出正在临界区执行写操作的提示信息
//在临界区中写操作,至少持续m_persist秒钟
printf("Writer thread %d begins to write file.\n", m_serial);
Sleep(m_persist);
printf("Writer thread %d finished writing file. \n",m_serial);
//释放临界资源
LeaveCriticalSection(&RP_Write);
}

// 读者优先――处理函数
void ReaderPriority(char *file) //file: 文件名
{
DWORD n_thread=0; //保存线程数目变量
DWORD thread_ID; //线程ID
DWORD wait_for_all; //保存――等待所有线程结束的返回值――的变量
HANDLE h_Thread[MAX_THREAD_NUM];
ThreadInfo thread_info[MAX_THREAD_NUM];
HANDLE h_Mutex=CreateMutex(NULL,FALSE,"mutex_for_readcount");

readcount=0;
InitializeCriticalSection(&RP_Write); //初始化临界区
ifstream inFile;
inFile.open(file);
printf("Reader Priority :\n\n");

while (inFile) { //读入每个读者、写者的信息
inFile>>thread_info[n_thread].serial;
inFile>>thread_info[n_thread].entity;
inFile>>thread_info[n_thread].delay;
inFile>>thread_info[n_thread].persist;
inFile.get();
n_thread++;
}
n_thread--; //modify
for(int j=0;j<(int)(n_thread); j++){
cout<<"threadinfo["<<j<<"]="<<thread_info[j].serial<<","<<thread_info[j].entity
<<","<<thread_info[j].delay<<","<<thread_info[j].persist<<endl;
}
for(int i=0; i<(int)(n_thread); i++){
//创建一个不被子进程继承的,使用默认大小堆栈的线程
if( thread_info[i].entity ==READER || thread_info[i].entity=='r')
h_Thread[i] = CreateThread(NULL, 0,
(LPTHREAD_START_ROUTINE)(RP_ReaderThread),
&thread_info[i],0,&thread_ID);
// thread_info[i]籍此传递给被创建线程,
// 创建标志=0, 表示线程被创建后立即执行 (其它:CREATE_SUSPEND)
//thread_ID 存放放回的线程ID标识, 可不用
else
h_Thread[i]=CreateThread(NULL, 0,
(LPTHREAD_START_ROUTINE)(RP_WriterThread),
&thread_info[i],0,&thread_ID);
}
//等待所有的线程结束
wait_for_all = WaitForMultipleObjects(n_thread, h_Thread, TRUE, -1);
printf("All reader and writer have finished operating. \n");
}

// 写者优先――读者线程
void WP_ReaderThread(void *p) //p: 读者线程信息
{
DWORD m_delay; //延迟时间
DWORD m_persist; //读文件持续时间
int m_serial; //线程序号

m_serial = ((ThreadInfo*)(p))->serial;
m_delay = (DWORD)((ThreadInfo*)(p))->delay*INTE_PER_SEC;
m_persist = (DWORD)((ThreadInfo*)(p))->persist*INTE_PER_SEC;
Sleep(m_delay);
printf ("Reader thread %d sends the reading require.\n", m_serial);
//延迟等待
//从参数中获取信息m_serial, m_delay, m_persist ,
//延迟等待m_delay秒;
//输出信息"读线程 %d 发出读请求的提示信息;

//打开(获取)两个互斥变量
HANDLE h_Mutex1, h_Mutex2;
h_Mutex1=OpenMutex(MUTEX_ALL_ACCESS, FALSE, "mutex_for_writecount1");
/*由于写者优先,即使临界区中已有读者,也不能马上进入,
要等到没有等待写者(在写进程中,当无等待写者释放读者临界区所有权cs_Read)
因此,这里需要一个互斥信号量附加控制读者进入过程,这在读者优先算法中是不需要的。*/

DWORD wait_for_mutex1=WaitForSingleObject(h_Mutex1,-1);
EnterCriticalSection(&cs_Read);
// 用h_mutex2控制对readcount的访问
h_Mutex2 = OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex_for_readcount2");
DWORD wait_for_mutex2 = WaitForSingleObject(h_Mutex2,-1);
readcount++;
if ( readcount==1) //第一个读者要等待写者写完
EnterCriticalSection(&cs_Write);
ReleaseMutex(h_Mutex2);

LeaveCriticalSection(&cs_Read); //让其它读者可进入临界区
ReleaseMutex(h_Mutex1);
//输出某读者进程在临界区中读的信息;
//在临界区中读操作,至少持续m_persist秒钟
//输出某读者进程退出临界区的信息;
printf("Reader thread %d begins to read file.\n", m_serial);
Sleep(m_persist);
//退出线程
printf("Reader thread %d finished reading file. \n",m_serial);
wait_for_mutex2 = WaitForSingleObject(h_Mutex2,-1);
readcount--;
if ( readcount==0) //最后一个读者,要唤醒写者
LeaveCriticalSection(&cs_Write);
ReleaseMutex(h_Mutex2);
}

// 写者优先――写者线程
void WP_WriterThread(void *p) //p: 写者线程信息
{
DWORD m_delay; //延迟时间
DWORD m_persist; //读文件持续时间
int m_serial; //线程序号
//从参数中获取信息m_serial, m_delay, m_persist ,
m_serial = ((ThreadInfo*)(p))->serial;
m_delay = (DWORD)((ThreadInfo*)(p))->delay*INTE_PER_SEC;
m_persist = (DWORD)((ThreadInfo*)(p))->persist*INTE_PER_SEC;

//延迟等待m_delay秒;
Sleep(m_delay);
//输出信息"写线程 %d 发出写请求;
printf ("Writer thread %d sends the writing require.\n", m_serial);
//打开(获取)两个互斥变量h_Mutex3
HANDLE h_Mutex3;
h_Mutex3 = OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex_for_writecount3");
DWORD wait_for_mutex3 = WaitForSingleObject(h_Mutex3,-1);
writecount++;
if (writecount==1) //第一个等待写者,等待读者读完
EnterCriticalSection(&cs_Read);
ReleaseMutex(h_Mutex3);

EnterCriticalSection(&cs_Write);
//进入写着临界区
//输出某写者进程在临界区中写的信息;
//至少持续m_persist秒钟
//输出某写者进程退出临界区的信息;
printf("Writer thread %d begins to write file.\n", m_serial);
Sleep(m_persist);
printf("Writer thread %d finished writing file. \n",m_serial);

LeaveCriticalSection(&cs_Write); //写者离开临界区
wait_for_mutex3 = WaitForSingleObject(h_Mutex3,-1);
writecount--;
if (writecount==0) //无其它等待写者,唤醒第一个读者
LeaveCriticalSection(&cs_Read);
ReleaseMutex(h_Mutex3);
}

// 写者优先――处理函数
void WriterPriority(char *file) //file: 文件名
{
DWORD n_thread=0, thread_ID, wait_for_all;
HANDLE h_Mutex1=CreateMutex(NULL,FALSE,"mutex_for_writecount1");
HANDLE h_Mutex2=CreateMutex(NULL,FALSE,"mutex_for_readcount2");
HANDLE h_Mutex3=CreateMutex(NULL,FALSE,"mutex_for_writecount3");
//用CreateMutex创建3个互斥信号量句柄h_mutex1,h_mutex2, h_mutex3;
//从数据文件读入信息,创建指定数目的读写进程
//读进程处理函数 WP_ReaderThread, 写进程处理函数WP_writerThread
HANDLE h_Thread[MAX_THREAD_NUM];
ThreadInfo thread_info[MAX_THREAD_NUM];
writecount=0;
readcount=0;
InitializeCriticalSection(&cs_Write);
InitializeCriticalSection(&cs_Read);
//初始化临界区
ifstream inFile;
inFile.open(file);
printf("Write Priority :\n\n");

while (inFile) { //读入每个读者、写者的信息
inFile>>thread_info[n_thread].serial;
inFile>>thread_info[n_thread].entity;
inFile>>thread_info[n_thread].delay;
inFile>>thread_info[n_thread].persist;
inFile.get();
n_thread++;
}
n_thread--;
for(int j=0;j<(int)(n_thread); j++){
cout<<"threadinfo["<<j<<"]="<<thread_info[j].serial<<","<<thread_info[j].entity
<<","<<thread_info[j].delay<<","<<thread_info[j].persist<<endl;
}
for(int i=0; i<(int)(n_thread); i++) {
//创建一个不被子进程继承的,使用默认大小堆栈的线程
if( thread_info[i].entity ==READER || thread_info[i].entity=='r' )
h_Thread[i] = CreateThread(NULL, 0,
(LPTHREAD_START_ROUTINE)(WP_ReaderThread),
&thread_info[i],0,&thread_ID);
// thread_info[i]籍此传递给被创建线程,
// 创建标志=0, 表示线程被创建后立即执行 (其它:CREATE_SUSPEND)
//thread_ID 存放放回的线程ID标识, 可不用
else
h_Thread[i]=CreateThread(NULL, 0,
(LPTHREAD_START_ROUTINE)(WP_WriterThread),
&thread_info[i],0,&thread_ID);
}
//等待所有的线程结束
wait_for_all = WaitForMultipleObjects(n_thread, h_Thread, TRUE, -1);
printf("All reader and writer have finished operating. \n");
}

///////////////////////////////////////////////////////////
///主函数
int main(int argc, char** argv)
{
char ch;
while (true)
{
//打印显示信息
printf("***************************************\n");
printf(" 1. Reader Priority \n");
printf(" 2. Writer Priority \n");
printf(" 3. Exit \n");
printf("***************************************\n");
printf("Enter your choice(1,2,or 3): \n");
do {
ch = (char)_getch();
} while (ch != '1' && ch != '2' && ch!='3');
system("cls");
if(ch=='3') return 0;
else if(ch=='1') ReaderPriority("thread.dat");
else if(ch=='2') WriterPriority("thread.dat");
//结束
printf("\nPress any key to continute:");
_getch();
system("cls");
}
return 1;
}

⑻ 编写一个多线程程序,同时启动2个线程,a线程打印1到100,b线程打印101到200

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
Thread thread1 = new Thread(Print1_100);
Thread thread2 = new Thread(Print101_200);

thread1.Start();
thread2.Start();

Console.Read();
}

private static void Print1_100()
{
Print(1, 100);
}

private static void Print101_200()
{
Print(101, 200);
}

private static void Print(int min, int max)
{
for (int i = min; i <= max; i++)
{
Console.WriteLine(i);
}
}
}
}

⑼ 多线程是什么意思啊运行多个程序的意思吗

多线程是指程序中包含多个执行流,即在一个程序中可以同时运行多个不同的线内程来执行容不同的任务,也就是说允许单个程序创建多个并执行的线程来完成各自的任务。
多线程的好处在于可以提高CPU的利用率。在多线程的程序中,当一个线程必须等待的时候,CPU可以运行其它的线程而不是等待,这样就大大提高了程序的效率。

阅读全文

与机械手多个线程程序相关的资料

热点内容
黑龙江特种设备检验研究院 浏览:210
机械化养护中心 浏览:838
上海特种设备管理 浏览:48
机械师改枪 浏览:181
机械化剪纸 浏览:757
美燃环保设备 浏览:809
济南北斗星数控设备有限公司 浏览:838
自动喷涂机械手 浏览:457
中小型农业机械加工项目建议书 浏览:251
不锈钢加工设备市转让 浏览:441
水稻生产全程机械化 浏览:110
扳手机械原理 浏览:61
凯格精密机械有限公司 浏览:61
广毅机电设备 浏览:805
重庆三阳办公设备有限公司 浏览:494
华技达自动化设备 浏览:631
东莞石碣自动化设备厂 浏览:131
机械制图陈列柜 浏览:246
郑州奥鑫游乐设备公司 浏览:733
美邦环保设备有限公司 浏览:386