導航:首頁 > 機械設備 > 機械手多個線程程序

機械手多個線程程序

發布時間: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